package com.shangyueshang.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.shangyueshang.common.SysConstants;
import com.shangyueshang.mapper.ReturnOrderMapper;
import com.shangyueshang.model.ReturnOrder;
import com.shangyueshang.request.PageReq;
import com.shangyueshang.response.PageResult;
import com.shangyueshang.service.ReturnOrderService;
import com.shangyueshang.utils.DateUtil;
import com.shangyueshang.utils.MyPageHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.time.YearMonth;
import java.util.List;
import java.util.Map;

/**
 * @author: JokeyZheng
 * @email: zhengjingfeng@ruqimobility.com
 * @created: 2022/1/14 19:06
 * @version: v1.0.0
 */
@Slf4j
@Service
public class ReturnOrderServiceImpl extends ServiceImpl<ReturnOrderMapper, ReturnOrder> implements ReturnOrderService {

    @Override
    public PageResult<ReturnOrder> findPage(PageReq pageReq) {
        QueryWrapper<ReturnOrder> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc(SysConstants.return_date);
        Map<String, Object> queryParams = pageReq.getLineParams();
        queryWrapper.in(SysConstants.shop, (List) queryParams.remove(SysConstants.shop));
        queryParams.forEach((k, v) -> {
            if (k.equals(SysConstants.return_month)) {
                //月份转为时间
                YearMonth month = DateUtil.parseYYYMM(v.toString());
                queryWrapper.ge(SysConstants.return_date, month.atDay(1));
                queryWrapper.le(SysConstants.return_date, month.atEndOfMonth());
            } else {
                queryWrapper.eq(k, v);
            }
        });
        PageInfo<ReturnOrder> pageInfo =
                PageHelper.startPage(
                        pageReq.getPageNum(),
                        pageReq.getPageSize()
                ).doSelectPageInfo(() -> super.list(queryWrapper));
        return MyPageHelper.getPageResult(pageInfo);
    }
}
